//
//  OrderListCell.swift
//  Eternity
//
//  Created by cll12_12 on 2016/12/26.
//  Copyright © 2016年 ZhengWei. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class OrderListCell: UITableViewCell {

    var rightConstraint: Constraint?
    
    // MAKR: - life cycle
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initBaseLayout()
        layoutPageSubviews()
        self.backgroundColor = UIColor.white
        self.selectionStyle = .none
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - private method
    func initBaseLayout() {
        self.addSubview(orderNumLabel)
        self.addSubview(statusLabel)
        self.addSubview(topLine)
        self.addSubview(middleLine)
        self.addSubview(totalLabel)
        self.addSubview(bottomLine)
        self.addSubview(goBtn)
        self.addSubview(checkBtn)
        self.addSubview(confirmBtn)
    }
    
    func layoutPageSubviews() {
        orderNumLabel.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(self).offset(11)
            make.width.equalTo(207)
            make.height.equalTo(20)
        }
        
        statusLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(self).offset(11)
            make.width.equalTo(87)
            make.height.equalTo(20)
        }
        
        topLine.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(statusLabel.snp.bottom).offset(9)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
        
        middleLine.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(topLine.snp.bottom).offset(141)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
        
        totalLabel.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(middleLine.snp.bottom).offset(15)
            make.width.equalTo(406)
            make.height.equalTo(10)
        }
        
        bottomLine.snp.makeConstraints { (make) in
            make.left.equalTo(self).offset(14)
            make.top.equalTo(totalLabel.snp.bottom).offset(14)
            make.right.equalTo(self).offset(0)
            make.height.equalTo(1)
        }
        
        goBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-14)
            make.top.equalTo(bottomLine.snp.bottom).offset(8)
            make.width.equalTo(75)
            make.height.equalTo(32)
        }
        
        checkBtn.snp.makeConstraints { (make) in
            rightConstraint = make.right.equalTo(self).offset(-109).constraint
            make.top.equalTo(bottomLine.snp.bottom).offset(8)
            make.width.equalTo(84)
            make.height.equalTo(32)
        }
        
        confirmBtn.snp.makeConstraints { (make) in
            make.right.equalTo(self).offset(-13)
            make.top.equalTo(bottomLine.snp.bottom).offset(8)
            make.width.equalTo(84)
            make.height.equalTo(32)
        }
    }
    
    func configUI(status: STATUS, section: Int, data: STATUS) {
        orderNumLabel.text = "订单号：HM16081616060001"
        totalLabel.text = "共计2件商品，实付款：￥1005.00(含运费：$0.00)"
        
        for i in 0 ..< 3 {
            let imageView = UIImageView(frame: CGRect(x: 14+108*i, y: 62, width: 100, height: 100))
            imageView.sd_setImage(with: URL(string: "http://img15.3lian.com/2015/h1/57/d/61.jpg"), placeholderImage: UIImage(named: "def_200x200"))
                
            self.addSubview(imageView)
        }
            
            switch status {
            case .WAITFORPAY:
                statusLabel.text = "等待付款"
                goBtn.isHidden = false
                break
            case .WAITFORGOODS:
                statusLabel.text = "待收货"
                checkBtn.isHidden = false
                confirmBtn.isHidden = false
                goBtn.isHidden = true
                break
            case .ALLORDER:
                switch data {
                case .CANCELED:
                    statusLabel.text = "已取消"
                    statusLabel.textColor = UIColor(hexString: "#999999")
                    checkBtn.isHidden = true
                    confirmBtn.isHidden = true
                    goBtn.isHidden = true
                    break
                case .FINISHED:
                    statusLabel.text = "已完成"
                    statusLabel.textColor = UIColor(hexString: "#252525")
                    checkBtn.isHidden = false
                    confirmBtn.isHidden = false
                    checkBtn.setTitle("再次购买", for: .normal)
                    confirmBtn.setTitle("申请售后", for: .normal)
                    break
                case .PAID:
                    statusLabel.text = "已付款"
                    checkBtn.isHidden = true
                    confirmBtn.isHidden = true
                    goBtn.isHidden = true
                    break
                case .WAITFORPAY:
                    statusLabel.text = "等待付款"
                    goBtn.isHidden = false
                    break
                case .WAITFORGOODS:
                    statusLabel.text = "待收货"
                    checkBtn.isHidden = false
                    confirmBtn.isHidden = false
                    goBtn.isHidden = true
                    break
                default:
                    break
                }
                break
            default:
                break
            }
    }
    
    // MARK: - setters and getters
    // 订单页面
    var _orderNumLabel: UILabel!
    var orderNumLabel: UILabel {
        if _orderNumLabel == nil {
            _orderNumLabel = UILabel()
            _orderNumLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _orderNumLabel.textColor = UIColor(hexString: "#666666")
        }
        return _orderNumLabel
    }
    
    var _statusLabel: UILabel!
    var statusLabel: UILabel {
        if _statusLabel == nil {
            _statusLabel = UILabel()
            _statusLabel = UILabel()
            _statusLabel.font = UIFont(name: "PingFangSC-Regular", size: 14)
            _statusLabel.textColor = UIColor(hexString: "#B50834")
            _statusLabel.textAlignment = .right
        }
        return _statusLabel
    }

    var _topLine: UIView!
    var topLine: UIView {
        if _topLine == nil {
            _topLine = UIView()
            _topLine.backgroundColor = UIColor(hexString: "#DDDDDD")
        }
        return _topLine
    }
    
    var _middleLine: UIView!
    var middleLine: UIView {
        if _middleLine == nil {
            _middleLine = UIView()
            _middleLine.backgroundColor = UIColor(hexString: "#DDDDDD")
        }
        return _middleLine
    }
    
    var _totalLabel: UILabel!
    var totalLabel: UILabel {
        if _totalLabel == nil {
            _totalLabel = UILabel()
            _totalLabel.font = UIFont(name: "PingFangSC-Regular", size: 10)
            _totalLabel.textColor = UIColor(hexString: "#000000")
            _totalLabel.textAlignment = .right
        }
        return _totalLabel
    }
    
    var _bottomLine: UIView!
    var bottomLine: UIView {
        if _bottomLine == nil {
            _bottomLine = UIView()
            _bottomLine.backgroundColor = UIColor(hexString: "#DDDDDD")
        }
        return _bottomLine
    }
    
    var _goBtn: UIButton!
    var goBtn: UIButton {
        if _goBtn == nil {
            _goBtn = UIButton()
            _goBtn.backgroundColor = UIColor(hexString: "#252525")
            _goBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
            _goBtn.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 12)
            _goBtn.setTitle("去支付", for: .normal)
        }
        return _goBtn
    }
    
    var _checkBtn: UIButton!
    var checkBtn: UIButton {
        if _checkBtn == nil {
            _checkBtn = UIButton()
            _checkBtn.backgroundColor = UIColor(hexString: "#FFFFFF")
            _checkBtn.setTitleColor(UIColor(hexString: "#252525"), for: .normal)
            _checkBtn.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 12)
            _checkBtn.layer.borderColor = UIColor(hexString: "#B7B7B7")?.cgColor
            _checkBtn.layer.borderWidth = 0.5
            _checkBtn.setTitle("查看物流", for: .normal)
            _checkBtn.isHidden = true
        }
        return _checkBtn
    }
    
    var _confirmBtn: UIButton!
    var confirmBtn: UIButton {
        if _confirmBtn == nil {
            _confirmBtn = UIButton()
            _confirmBtn.backgroundColor = UIColor(hexString: "#252525")
            _confirmBtn.setTitleColor(UIColor(hexString: "#FFFFFF"), for: .normal)
            _confirmBtn.titleLabel?.font = UIFont(name: "PingFang-SC-Regular", size: 12)
            _confirmBtn.setTitle("确认收货", for: .normal)
            _confirmBtn.isHidden = true
        }
        return _confirmBtn
    }
    
    // 售后服务页面
    

    
}
